<?
    // Final

    class Principio_Model_TypeExtTemplateMapper extends Principio_Library_Mapper
    {
        private static $db_table = null;

        private static function getDBTable()
        {
            if (self::$db_table == null)
                self::$db_table = new Principio_Model_DbTable_TypeExtTemplate();

            return self::$db_table;
        }


        public static function delete($type_id, $use_transaction = false)
        {
            try
            {
                if ($use_transaction)
                    self::getDBTable()->getAdapter()->beginTransaction();

                self::getDBTable()->delete(array("type_id=?" => intval($type_id)));

                if ($use_transaction)
                    self::getDBTable()->getAdapter()->commit();

                return true;
            }
            catch (Exception $ex)
            {
                $return = self::reportException($ex);

                if ($use_transaction)
                {
                    self::getDBTable()->getAdapter()->rollback();
                    return false;
                }
                else
                    return $return;

            };
        }


        public static function update(Principio_Model_TypeExtTemplate &$model_type_ext_template)
        {
            try
            {
                $type_id = $model_type_ext_template->getTypeId();

                self::getDBTable()->getAdapter()->beginTransaction();

                for ($i = 0; $i < $model_type_ext_template->getExts(); $i++)
                    self::getDBTable()->update(array("template" => $model_type_ext_template->getTemplate($i)), array("type_id=?" => $type_id, "ext_number=?" => $i));

                self::getDBTable()->getAdapter()->commit();

            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                self::getDBTable()->getAdapter()->rollBack();

                return false;
            };
        }


        public static function insert(Principio_Model_TypeExtTemplate &$model_type_ext_template)
        {
            $type_id = $model_type_ext_template->getTypeId();

            for ($i = 0; $i < $model_type_ext_template->getExts(); $i++)
                self::getDBTable()->insert(array("type_id" => $type_id, "ext_number" => $i, "template" => $model_type_ext_template->getTemplate($i)));
        }


        public static function get($type_id)
        {
            try
            {
                $select = self::getDBTable()->select()->where("type_id=?", $type_id);
                $values = self::getDBTable()->fetchAll($select);

                return new Principio_Model_TypeExtTemplate($values->toArray());
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }

        public static function search($string)
        {
            $search_fields = array(
                "template" => "Template"
            );

            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);

            foreach ($result as $res)
            {
                $type_info = Principio_Model_TypeMapper::get($res["type_id"]);
                $model_search = new Principio_Model_Search($string, "Spa types");
                $model_search->setId($res["type_id"])->setResultName($type_info->getCaption())->setOpenItemFunctionName("SpaTypes")->setParentID(false);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                        $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["type_id"]] = $model_search;
            }
            return $search;

        }
    }
?>